package com.hy.demo.learn.thread;

/**
 * <p>
 *
 * </p>
 *
 * @author cjl
 * @version 1.0
 * @since 2020-08-27 09:19
 */

class  MyThreadTest implements Runnable{

    private volatile int ticket=5;


    @Override
    public void run() {
        synchronized (this) {
            while (this.ticket > 0) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在卖票" + this.ticket--);
            }

        }
    }
}

public class VolatileTest {
    public static void main(String[] args) {

        MyThreadTest myThreadTest=new MyThreadTest();
        new Thread(myThreadTest,"票贩子A").start();
        new Thread(myThreadTest,"票贩子B").start();
        new Thread(myThreadTest,"票贩子C").start();
    }
}
